package com.example.chartview

import android.content.Context
import android.graphics.*
import android.support.v4.view.GestureDetectorCompat
import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import java.text.NumberFormat

class ClientChartMeView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rectW = 40f
    private var viewWidth = 0
    private var rectOffset = 0F
    private val textRect = Rect()
    private val textYOffset = 10F
    private var list = mutableListOf<Double>()
    private val topOffset = 200F
    private val kingOffset = 30F
    private val showCount = 8
    private val allCount = 8
    private var maxValue = 0.0

    private val gestureDetectorCompat: GestureDetectorCompat =
            GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
                override fun onDown(e: MotionEvent?): Boolean = true
                override fun onScroll(
                        e1: MotionEvent?,
                        e2: MotionEvent?,
                        distanceX: Float,
                        distanceY: Float
                ): Boolean {
                    scrollBy(distanceX.toInt(), 0)
                    //  判断临界值
                    if (scrollX < 0) {
                        scrollX = 0
                    }
                    if (scrollX > viewWidth - width) {
                        scrollX = viewWidth - width
                    }
                    return false
                }
            })
    lateinit var linearGradient: LinearGradient

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        viewWidth = width * allCount / showCount
        rectOffset = viewWidth / allCount * 0.5F
        linearGradient = LinearGradient(
                0F,
                0F,
                rectW,
                height.toFloat(),
                Color.parseColor("#FF1372F0"),
                Color.parseColor("#001372F0"),
                Shader.TileMode.MIRROR
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        var pos = 0
        val bottomN = height.toFloat() - 50F
        for (index in list.indices) {


            val leftN = viewWidth / allCount * index + rectOffset +80
            var max = rectW / 2 - bottomN
            var singleHight = max * (list[index] / maxValue)
            Log.d("singleHight", singleHight.toString())
            val topN = bottomN + singleHight.toFloat()
            val rightN = viewWidth / allCount * index + rectW + rectOffset

            paint.shader = linearGradient
            //  画长方形
//            canvas.drawRect(leftN, topN, rightN, bottomN, paint)
//            canvas.drawCircle(leftN + (rightN - leftN) / 2, topN, (rightN - leftN) / 2, paint)
//            canvas.drawArc(leftN, topN - 20, rightN, topN + 20, 20f, 20f
//                    , true, paint)
            canvas.drawRoundRect(leftN, topN - 20, rightN, bottomN, 10f, 10f, paint)

            //  画底部数字
            paint.apply {
                val text = (index + 1).toString()
                shader = null
                color = Color.WHITE
                textSize = 35F
                textAlign = Paint.Align.CENTER
                getTextBounds(text, 0, text.length, textRect)
                canvas.drawText(
                        text,
                        leftN + rectW / 2 - 40,
                        bottomN + textRect.height() + textYOffset,
                        paint
                )


                canvas.drawText(list[index].toString(),leftN + rectW / 2 - 40, topN - kingOffset ,paint)
                println("【IM】SingleView2  onDraw: ${textRect.height()}")
            }

            paint.apply {
                textAlign = Paint.Align.CENTER
                textSize = 25f
                shader = null
                color = Color.WHITE
                getTextBounds("1000", 0, 4, textRect)
            }
            val maxTextWidth = (textRect.right - textRect.left).toFloat()
            for (i in 0..4) {

                var text =((i * 0.25) * maxValue).toFloat()
                val nf: NumberFormat = NumberFormat.getNumberInstance()
                nf.minimumFractionDigits = 2


                var btmIndex = (max / 4 )* i
                canvas.drawText(
                        nf.format(text),
                        maxTextWidth,
                        bottomN + btmIndex,
                        paint
                )


            }
        }
        //  底线
        paint.color = Color.WHITE
        canvas.drawLine(80f, bottomN, viewWidth.toFloat(), bottomN, paint)
        //  我不知道具体有几个刻度 就照四个刻度平均分配

//        canvas.drawLine(0F, bottomN, viewWidth.toFloat(), bottomN, paint)
//        //  我不知道具体有几个刻度 就照四个刻度平均分配
//        paint.textAlign = Paint.Align.CENTER
//        paint.textSize = 14F
//
//        for (i in 1..4) {
//            paint.apply {
//                val text = (i * 0.25) * maxValue
//                shader = null
//                color = Color.WHITE
//                textSize = 30f
//                textAlign = Paint.Align.CENTER
//                getTextBounds(text.toString(), 0, text.toString().length, textRect)
//                canvas.drawText(
//                        text.toString(),
//                        (textRect.right - textRect.left).toFloat(),
//                        bottomN - bottomN / 5 * i,
//                        paint
//                )
//            }
//
//        }

    }


    override fun onTouchEvent(event: MotionEvent?): Boolean =
            gestureDetectorCompat.onTouchEvent(event)

    fun setList(listData: MutableList<Double>, max: Double) {
        this.list = listData
        this.maxValue = max
        invalidate()
    }
}